// Main CE Atlas application initialization component.
$.extend(CE, {

    GUID: 1000000,

    //constants.
    START_YEAR: 1970,
    END_YEAR: 2020,

    ORIGIN: CE.UTIL.point(-125.8942, 49.154),
    
    selected_feature: {
        id: ''
    },
	
    // get an ID which will be globally unique from other IDs selected using this method.
    getGuid: function () {
        this.GUID += 1;
        return '' + this.GUID;
    },

    // global initialization
    init: function () {

        this.$map = $("#ce_map");
        this.$tabs = $(".tab-content");
        
        $('#everything1').fadeOut(1);
        
        // refresh the layout on window resize (and during initialization)
        $(window).resize(CE.reset_layout).resize();
        CE.init_map();
        $(window).resize();
        
        $(".ce-ui").css('visibility', 'visible');
        
        CE.THEME.init();
        CE.PLACE.init();
        CE.MEDIA.init();
        CE.MEDIA.load();
        CE.WIDGETS.init();
        CE.UTIL.init();
        
        // initialize the feature tooltip widget for use.
        CE.$tool = $("#ce_tooltip");
        // handle feature icon clicks.
        
        $('#ce_dialog').live('click',function(evt){CE.WIDGETS.DIALOG.on_click(evt);});
        $('.menu').live('click',CE.WIDGETS.MENU.open.createDelegate(CE.WIDGETS.MENU));
        $('.ce-menu-div').live('click',CE.WIDGETS.MENU.click.createDelegate(CE.WIDGETS.MENU));
        $('input').live('click', function(evt) {
            if (evt.target.id === 'search_media') {
                CE.MEDIA.search(evt);
            } else if (evt.target.id === 'add_media') {
                CE.MEDIA.ADD.dialog(true);
            }
        });

        $().click(function (evt) {
            // Get the element associated with the click.
            var $tgt = $(evt.target);
            if ($tgt.parents('.ce-menu-div').length || $tgt.filter('.menu').length) {
                return;
            } else {
                CE.WIDGETS.MENU.close_all();
            }
        });
        
        // reset the layout (for the first time to initialize it)
        
        CE.reset_layout.after(function(){
            CE.WIDGETS.SLIDER.on_resize();
        });
        
        CE.TOUR.init();
        
        //CE.LAYERS.georss.load();
        CE.LAYERS.wms.load();
        CE.LAYERS.wfs.load();
        
        // ie doesn't play nice with the header animation.
        if (! $.browser.msie)
            {setTimeout(CE.header_animation.run,3000);}
        //$(window).resize();
    },
    
    header_animation: {
    
        state:false,
        
        run:function() {
            CE.header_animation.state = !CE.header_animation.state;
            $('#everything0,#everything1').unbind();
            if (CE.header_animation.state) {
                $('#everything0').fadeOut(3000,CE.header_animation.reset);
                $('#everything1').fadeIn(3000);
            } else {
                $('#everything1').fadeOut(3000,CE.header_animation.reset);
                $('#everything0').fadeIn(3000);
            }
        },
        
        reset: function() {
            $('#everything0,#everything1').bind('mouseover',CE.header_animation.run);
        }
    },
    
    reset_layout: AOP(function () {
        
        var HEADER_HEIGHT = 60
        var map_height = $(window).height() - HEADER_HEIGHT; 
        var left_col = ($(window).width() - $("#ce_right").outerWidth());

        $('#ce_map').css({
            height: map_height + 'px',
            width: left_col + 'px'
        });
        
        $('#ce_right').css({
            height: map_height + 'px'
        });
        
        $('#ce_slider_rulings table').css({
            width: left_col + 'px'
        });
    
        $('.olControlScaleLine').css({
            left: left_col - 70 + 'px'
        });
       /* 
        var $media_tab = $('#ce_tab_media')
        var media_btm = $media_tab.offset().top + $media_tab.height() - HEADER_HEIGHT;
        console.log( map_height - media_btm - 65 );
        $('#ce_tab_media_content').css('height', map_height - media_btm - 65 +'px');
        console.log($('#ce_tab_media_content').height()); 
        */
        if (CE.map) {
            CE.controls.scale_line.moveTo(new OpenLayers.Pixel(CE.$map.width() - 100, CE.$map.height() - 30));
        }
        
        CE.MEDIA.list.draw(); // media items won't be in the right spot on the time bar anymore.
    })
});
